﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
//using System.Math;
using Microsoft.Kinect;
using Microsoft.Win32;
using Kinect.Toolbox;
using Kinect.Toolbox.Gestures;
using System.Threading;
using System.Threading.Tasks;
using Kinect.Toolbox.Record;
using Kinectver1._0;
using Kinectver1._0.CadObject;
using Apex.MVVM;
using SharpGL.SceneGraph;
using SharpGL;
using SharpGL.SceneGraph.Cameras;
using SharpGL.SceneGraph.Lighting;
using SharpGL.SceneGraph.Effects;
using SharpGL.SceneGraph.Core;
using SharpGL.SceneGraph.Primitives;
using SharpGL.SceneGraph.Quadrics;
using SharpGL.SceneGraph.Helpers;
using SharpGL.Enumerations;

namespace Kinectver1._0
{
    //this will be our baby for diplaying obj's
    //our gl driver
    //and coordinate plane organizer
    class CoordinatePlane : ViewModel
    {

        public CoordinatePlane()
        {
            camReady = true;
            CamPos = new Vertex(0.0f, 0.0f, 0.0f);
            createScene();
            initworkspace();

        }
        private void initworkspace()
        {
            //create a folder
            var nfolder = new Folder()
            {
                Name = "KTACMS-PROJECT"
            };
            workspace.SceneContainer.AddChild(nfolder);

            foreach(SceneElement x in workspace.SceneContainer.Children)
            {
                if (x.Name == "KTACMS-PROJECT")
                    objectsFolder = x;
            }


        }
        private void createScene()
        {
            //  Create the perspective camera.

            //Camera = new LookAtCamera();

            //  Create the look at camera.
            var camera = new LookAtCamera()
            {
                Name = "Look at camera",
                Position = new Vertex(10, 10, 10),
                Target = new Vertex(0, 0, 0),
                UpVector = new Vertex(0, 0, 1)
            };
            
            Camera = camera;
            Camera.Near = 1.0f;
            Camera.Far = 300.0f;


            CamPos = Camera.Position;
            TarPos = Camera.Target;
            //  Create OpenGL.
            gl.Create(RenderContextType.DIBSection, 800, 600, 32, null);

            //   Create the scene object.
            workspace = new Scene() { OpenGL = gl };

            //  Initialise the scene.
            SceneHelper.InitialiseModelingScene(workspace); //adds 3 lights and some folders
            workspace.CurrentCamera = camera;

            //  Set fill in the polygon mode.
            openGlAttributes = (workspace.SceneContainer.Effects[0] as OpenGLAttributesEffect);
            openGlAttributes.PolygonAttributes.PolygonMode = PolygonMode.Filled;

            //  Set the attributes as a scene level effect.
            workspace.SceneContainer.AddEffect(openGlAttributes);
        }



        private NotifyingProperty SceneProperty =
          new NotifyingProperty("Scene2", typeof(Scene), default(Scene));

        public Scene workspace
        {
            get { return (Scene)GetValue(SceneProperty); }
            set { SetValue(SceneProperty, value); }
        }


        private NotifyingProperty CameraProperty =
          new NotifyingProperty("Camera", typeof(Camera), default(Camera));

        public LookAtCamera Camera
        {
            get { return (LookAtCamera)GetValue(CameraProperty); }
            set { SetValue(CameraProperty, value); }
        }
        //
        public Vertex CamPos { get; set; }
        public Vertex TarPos { get; set; }
        //hand updating function

        //movaable/ready to edit//not fixed, if its false, it is a fixed camera
        //original scale
        public float scaleX  {get; set;}
        public float scaleY { get; set; }
        public float scaleZ { get; set; }
        public bool camReady { get; set; }
        public SceneElement objectsFolder { get; set;}

        private OpenGL gl = new OpenGL();

        private OpenGLAttributesEffect openGlAttributes = new OpenGLAttributesEffect();


        private NotifyingProperty PolygonModeProperty =
          new NotifyingProperty("PolygonMode", typeof(PolygonMode), PolygonMode.Lines);

        public PolygonMode PolygonMode
        {
            get { return (PolygonMode)GetValue(PolygonModeProperty); }
            set
            {
                SetValue(PolygonModeProperty, value);
                openGlAttributes.PolygonAttributes.PolygonMode = PolygonMode;
            }
        }
        public void updater()
        {
            Camera.Position = CamPos;
        }

        public void move_x_f()
        {
            this.CamPos += new Vertex (1.0f,0.0f,0.0f);
            updater();
        }
        public void move_y_f()
        {
            this.CamPos += new Vertex(0.0f, 1.0f, 0.0f);
            updater();
        }
        public void move_z_f()
        {
            this.CamPos += new Vertex(0.0f, 0.0f, 1.0f);
            updater();
        }
        public void move_x_b()
        {
            this.CamPos -= new Vertex(1.0f, 0.0f, 0.0f);
            updater();
        }
        public void move_y_b()
        {
            this.CamPos -= new Vertex(0.0f, 1.0f, 0.0f);
            updater();
        }
        public void move_z_b()
        {
            this.CamPos -= new Vertex(0.0f, 0.0f, 1.0f);
            updater();
        }
        public void rotate_clockwise()
        {
            this.CamPos = new Vertex(CamPos.Y, -(CamPos.X), CamPos.Z);
            updater();
        }
        public void rotate_counterclockwise()
        {
            this.CamPos = new Vertex(-(CamPos.Y), CamPos.X, CamPos.Z);
            updater();
        }
        //public void doRotate(int type, int axis)
        //{
        //    //type 
        //    //0 for reset
        //    //1 90degree cw
        //    //2 90 degree ccw
        //    //3 180
        //    //axis
        //    //0x
        //    //1y
        //    //2z
        //}
    }
}
